import 'package:dartx/dartx.dart';
import 'package:get/get.dart';
import 'package:gluz/app/common/native_bridge.dart';
import 'package:gluz/app/data/user_profile_entity.dart';
import 'package:gluz/app/routes/app_pages.dart';
import 'package:gluz/app/utils/toast_util.dart';

import '../../../../app_session.dart';

class ProfileController extends GetxController {
  // 用户信息
  final userName = '菜鸟国际运输物流有限公司'.obs;
  final userAvatar = ''.obs;
  final authStatus = ''.obs;

  // 会员信息
  final memberLevel = 0.obs;
  final memberLevelString = 'Lv.1'.obs;
  final memberType = '钻石会员'.obs;
  final memberExpiryDate = '2025-12-30'.obs;
  
  // 积分信息
  final pointsBalance = 20000.obs;

  String get vipBgImage {
    return 'assets/images/mine/mine_vip_${memberLevel.value}.png';
  }

  String get vipCardImage {
    switch (memberLevel.value) {
      case 0:
        return app.isLangRU()
            ? 'assets/images/mine/mine_vip_card_${memberLevel.value}_ru.png'
            : 'assets/images/mine/mine_vip_card_${memberLevel.value}_zh.png';
      default:
        return 'assets/images/mine/mine_vip_card_${memberLevel.value}.png';
    }
  }

  @override
  void onInit() {
    super.onInit();
    loadUserData();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  // 加载用户数据
  Future<void> loadUserData() async {
    updateUIWithUser(app.getUser());
    UserProfileEntity? user = await app.getUserFromServer();
    await app.getUserVipEntity();
    updateUIWithUser(user);
  }

  void updateUIWithUser(UserProfileEntity? user) {
    if (user != null) {
      userAvatar.value = user.avatar;
      userName.value = user.nickName;
      authStatus.value = user.authStatusString();
      pointsBalance.value = user.vipEntity.score;
      memberLevel.value = user.vipEntity.level.toInt();
      memberLevelString.value = 'Lv.${user.vipEntity.level}';
      if (memberLevel.value == 1) {
        memberType.value = "黄金会员".tr;
      } else if (memberLevel.value == 2) {
        memberType.value = "铂金会员".tr;
      } else if (memberLevel.value == 3) {
        memberType.value = "钻石会员".tr;
      }
      memberExpiryDate.value = user.vipEntity.expiredDateString;
      update();
    }
  }

  // 跳转到设置页面
  void navigateToSettings() {
    // TODO: 跳转到设置页面
    app.logout();
    // app.translationToRU();
  }

  // 跳转到认证页面
  void onTapAuth() {
    app.jumpAuth();
  }

  // 跳转到积分商城
  void navigateToPointsMall() {
    // TODO: 跳转到积分商城
  }

  // 跳转到充值积分
  void navigateToRechargePoints() {
    // TODO: 跳转到充值积分
  }

  // 跳转到我的发布
  void navigateToMyPosts() {
    // TODO: 跳转到我的发布
  }

  // 跳转到我的邀请
  void navigateToMyInvitations() {
    // TODO: 跳转到我的邀请
  }

  // 跳转到我的收藏
  void navigateToMyFavorites() {
    // TODO: 跳转到我的收藏
  }

  // 跳转到服务订单
  void navigateToServiceOrders() {
    // TODO: 跳转到服务订单
  }

  // 跳转到身份认证
  void navigateToIdentityVerification() {
    // TODO: 跳转到身份认证
  }

  // 跳转到我的客服
  void navigateToCustomerService() {
    // TODO: 跳转到我的客服
  }

  // 跳转到投诉建议
  void navigateToComplaintsSuggestions() {
    // TODO: 跳转到投诉建议
  }

  // 跳转到积分账单
  void navigateToPointsBill() {
    // TODO: 跳转到积分账单
  }

  // 跳转到视频教程
  void navigateToVideoTutorials() {
    // TODO: 跳转到视频教程
  }
}
